﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using STOCK.Model;
using STOCK.DAL;
using Microsoft.Win32;
using System.IO;

namespace STOCK
{
    /// <summary>
    /// FNCBOOK.xaml 的交互逻辑
    /// </summary>
    public partial class FNCBOOK : Window
    {
        public FNCBOOK()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            lbCat.ItemsSource = new T_CategoryDAL().ListAll();
            cbEq.IsChecked = true;
            cbEnter.IsChecked = true;
            lbCat.SelectedIndex = 0;
            lbFun.SelectedIndex = 0;
        }

        private void lbCat_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_Category cate =(T_Category) lbCat.SelectedItem;
            lbFun.ItemsSource = new T_FunctionDAL().GetByCatId(cate.Id);
            lbFunName.ItemsSource = lbFun.ItemsSource;
            
        }

        private void lbFun_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_Function function = (T_Function)lbFun.SelectedItem;
            if (function != null)
            {
                txtDiscription.Text = new T_FunctionDAL().GetById(function.Id).Discription;
                lbFunName.SelectedItem = lbFun.SelectedItem;
                lbFunName.ScrollIntoView(function);
            }
        }

        private void lbFunName_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           T_Function function = (T_Function)lbFunName.SelectedItem;
            InsertFun(function);
        }

        private void InsertFun(T_Function function)
        {
            
            if (function != null)
            {

                if (cbEnter.IsChecked == false)//不换行
                {
                    if (cbEq.IsChecked == true)
                    {
                        txtFNCContent.Text += function.Name + ":=" + function.Fun;
                    }
                    else
                    {
                        txtFNCContent.Text += function.Fun;
                    }
                }
                else//换行
                {
                    if (txtFNCContent.Text.Length > 0)
                    {
                        txtFNCContent.Text += "\r\n";
                    }
                        if (cbEq.IsChecked == true)
                        {

                            txtFNCContent.Text += function.Name + ":=" + function.Fun+";";
                        }
                        else
                        {
                            txtFNCContent.Text += function.Fun+";";
                        }

                }
            }
        }

        private void lbFunName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_Function function = (T_Function)lbFunName.SelectedItem;
            if (function != null)
            {
                txtDiscription.Text = new T_FunctionDAL().GetById(function.Id).Discription;
                lbFun.SelectedItem = lbFunName.SelectedItem;
                lbFun.ScrollIntoView(function);
            }
        }

        private void lbFun_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            T_Function function = (T_Function)lbFun.SelectedItem;
            InsertFun(function);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            txtFNCContent.Text = "";
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtSaveRoot.Text.Length > 0 & txtFNCName.Text.Length > 0)
            {
                string filename = txtSaveRoot.Text + "\\" + txtFNCName.Text+".txt";
                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.WriteLine(txtFNCContent.Text);
                    }
                }
            }
            else
            {
                SaveFileDialog MyFilePath = new SaveFileDialog();
                MyFilePath.Filter = "txt文件|*.txt";
                if (MyFilePath.ShowDialog() != null)
                {
                    string filename = MyFilePath.FileName;
                    txtSaveRoot.Text = System.IO.Path.GetDirectoryName(filename);
                    txtFNCName.Text = System.IO.Path.GetFileNameWithoutExtension(filename);
                    using (FileStream fs = new FileStream(filename, FileMode.Create))
                    {
                        using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                        {
                            sw.WriteLine(txtFNCContent.Text);
                        }
                    }
                }
            }

        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "txt文件|*.txt";
            if (ofd.ShowDialog() != null)
            {
                string filename = ofd.FileName;
                txtSaveRoot.Text = System.IO.Path.GetDirectoryName(filename);
                txtFNCName.Text = System.IO.Path.GetFileNameWithoutExtension(filename);
                using (FileStream fs = new FileStream(filename,FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        txtFNCContent.Text = "";
                        string text =string.Empty;
                        while (!sr.EndOfStream)
                        {
                            text = sr.ReadLine();
                            txtFNCContent.Text += text + Environment.NewLine;
                        }
                    }
                }
            }
        }

    }
}
